package service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import param.lock.Lockrequest;
import param.lock.Lockresponse;
import param.orderquery.Orderqueryrequest;
import param.orderquery.Orderqueryresponse;
import param.pub.Publicresponse;
import param.residueseatquery.Residueseatqueryrequest;
import param.residueseatquery.Residueseatqueryresponse;
import param.returnticket.Returnrequest;
import param.returnticket.Returnresponse;
import param.sell.Sellrequest;
import param.sell.Sellresponse;
import param.taketicket.Taketicketrequest;
import param.taketicket.Taketicketresponse;
import param.testcon.Testconresponse;
import param.unlock.Unlockrequest;
import param.unlock.Unlockresponse;
import service.InvokeService;
import utils.PublicInfoUtil;
import utils.PublicXmlUtil;
import utils.XmlUtil;
import cn.nova.bus.ws.NetTicketSellServiceImplProxy;

public class NMGFInvokeServiceImpl implements InvokeService{

	final private Logger log = LoggerFactory.getLogger(getClass());

	private String webservice = "/NetTicketSellServiceImplService/NetTicketSellServiceImpl";

	@Override
	public Lockresponse lock(String serversaddress, Lockrequest obj) {
		Lockresponse response = new Lockresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
			resultXml = proxy.lock(xmlString);
			response = (Lockresponse) XmlUtil.toBean(resultXml, Lockresponse.class);
			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			if (response.getPublicresponse().getResultcode() == null) {
				response.getPublicresponse().setResultcode("NALOCK1007");
				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
			}
			log.error("内蒙一期锁位调用出错", e);
			log.error("锁位请求字符串：" + xmlString);
			log.error("锁位返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Residueseatqueryresponse residueseatquery(String serversaddress, Residueseatqueryrequest obj) {
		Residueseatqueryresponse response = new Residueseatqueryresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
			resultXml = proxy.residueseatquery(xmlString);
			response = (Residueseatqueryresponse) XmlUtil.toBean(resultXml, Residueseatqueryresponse.class);
			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			if (response.getPublicresponse().getResultcode() == null) {
				response.getPublicresponse().setResultcode("NARDQR1006");
				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
			}
			log.error("内蒙一期余票查询调用出错", e);
			log.error("余票查询请求字符串：" + xmlString);
			log.error("余票查询返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Unlockresponse unlock(String serversaddress, Unlockrequest obj) {
		Unlockresponse response = new Unlockresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
			resultXml = proxy.unlock(xmlString);
			response = (Unlockresponse) XmlUtil.toBean(resultXml, Unlockresponse.class);
			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			if (response.getPublicresponse().getResultcode() == null) {
				response.getPublicresponse().setResultcode("NAULCK1001");
				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
			}
			log.error("内蒙一期解锁调用出错", e);
			log.error("解锁请求字符串：" + xmlString);
			log.error("解锁返回字符串：" + resultXml);
		}
		return response;		
	}

	@Override
	public Sellresponse sell(String serversaddress, Sellrequest obj) {
		Sellresponse response = new Sellresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
			resultXml = proxy.sell(xmlString);
			response = (Sellresponse) XmlUtil.toBean(resultXml, Sellresponse.class);
			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			if (response.getPublicresponse().getResultcode() == null) {
				response.getPublicresponse().setResultcode("NASELL1005");
				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
			}
			log.error("内蒙一期售票调用出错", e);
			log.error("售票请求字符串：" + xmlString);
			log.error("售票返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Orderqueryresponse orderquery(String serversaddress, Orderqueryrequest obj) {
		Orderqueryresponse response = new Orderqueryresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
			resultXml = proxy.orderquery(xmlString);
			response = (Orderqueryresponse) XmlUtil.toBean(resultXml, Orderqueryresponse.class);
			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			if (response.getPublicresponse().getResultcode() == null) {
				response.getPublicresponse().setResultcode("NAODQR1006");
				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
			}
			log.error("内蒙一期订单查询调用出错", e);
			log.error("订单查询请求字符串：" + xmlString);
			log.error("订单查询返回字符串：" + resultXml);
		}
		return response;
	}

	@Override
	public Taketicketresponse taketicket(String serversaddress, Taketicketrequest obj) {
		Taketicketresponse response = new Taketicketresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
			resultXml = proxy.taketicket(xmlString);
			response = (Taketicketresponse) XmlUtil.toBean(resultXml, Taketicketresponse.class);
			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			if (response.getPublicresponse().getResultcode() == null) {
				response.getPublicresponse().setResultcode("NATKQR1003");
				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
			}
			log.error("内蒙一期取票调用出错", e);
			log.error("取票请求字符串：" + xmlString);
			log.error("取票返回字符串：" + resultXml);
		}
		return response;
	}

//	@Override
//	public Changeticketnoresponse changeticketno(String serversaddress, Changeticketnorequest obj) {
//		Changeticketnoresponse response = new Changeticketnoresponse();
//		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
//		String xmlString = null;
//		String resultXml = null;
//		try {
//			xmlString = PublicInfoUtil.toXml(obj);
//			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
//			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
//			resultXml = proxy.changeticketno(xmlString);
//			response = (Changeticketnoresponse) XmlUtil.toBean(resultXml, Changeticketnoresponse.class);
//			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
//			response.setPublicresponse(publicresponse);
//		} catch (Exception e) {
//			if (response.getPublicresponse().getResultcode() == null) {
//				response.getPublicresponse().setResultcode("NACTKN1003");
//				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
//			}
//			log.error("内蒙一期换票号重打印调用出错", e);
//			log.error("换票号重打印请求字符串：" + xmlString);
//			log.error("换票号重打印返回字符串：" + resultXml);
//		}
//		return response;
//	}
//
//	@Override
//	public Testconresponse saletestcon(String serversaddress) {
//		Testconresponse response = new Testconresponse();
//		Publicresponse publicresponse = PublicXmlUtil.newPublicresponse();
//		response.setPublicresponse(publicresponse);
//		try {
//			URL url = new URL("http://" + serversaddress + webservice + "?wsdl");
//			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//			conn.setRequestMethod("POST");// 提交模式
//			conn.setDoOutput(true);
//			conn.setDoInput(true);
//			conn.setUseCaches(false);
//			conn.setConnectTimeout(60000);
//
//			new DataOutputStream(conn.getOutputStream());
//		} catch (Exception e) {
//			response.getPublicresponse().setResultcode("NATEST1002");
//			response.getPublicresponse().setResultmsg("业务连通性接口调用失败" + e);
//			log.error(serversaddress + "一期业务连通性接口调用失败", e);
//		}
//		return response;
//	}

	@Override
	public Returnresponse returnticket(String serversaddress, Returnrequest obj) {
		Returnresponse response = new Returnresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String xmlString = null;
		String resultXml = null;
		try {
			xmlString = PublicInfoUtil.toXml(obj);
			String netTicketSellServiceImplPort_address = "http://" + serversaddress + webservice;
			NetTicketSellServiceImplProxy proxy = new NetTicketSellServiceImplProxy(netTicketSellServiceImplPort_address, true);
			resultXml = proxy.returnticket(xmlString);
			response = (Returnresponse) XmlUtil.toBean(resultXml, Returnresponse.class);
			Publicresponse publicresponse = DateFormat(response.getPublicresponse());
			response.setPublicresponse(publicresponse);
		} catch (Exception e) {
			if (response.getPublicresponse().getResultcode() == null) {
				response.getPublicresponse().setResultcode("NARTTK1003");
				response.getPublicresponse().setResultmsg("调用车站接口失败" + e);
			}
			log.error("内蒙一期退票调用出错", e);
			log.error("退票请求字符串：" + xmlString);
			log.error("退票返回字符串：" + resultXml);
		}
		return response;
	}


	private Publicresponse DateFormat(Publicresponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String returntime = response.getReturntime();
		try {
			response.setReturntime(sdf.format((sdf.parse(returntime))));
		} catch (ParseException e) {
			log.error("退票时间日期类型转换失败", e);
		}
		return response;
	}

	@Override
	public Testconresponse synctestcon(String serversaddress) {
		Testconresponse response = new Testconresponse();
		Publicresponse publicresponse = PublicXmlUtil.newPublicresponse();
		response.setPublicresponse(publicresponse);
		return response;
	}

	@Override
	public Returnresponse specialreturnticket(String serversaddress,
			Returnrequest obj) throws Exception {
		
		return null;
	}

	@Override
	public Testconresponse saletestcon(String serversaddress) {
		
		return null;
	}

}
